package dao

import (
	."bc/util"
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)
var db *gorm.DB

func InitDb(){
	var (
		err error
	)

	user := Config.DB.User
	password := Config.DB.Password
	host := Config.DB.Host
	dbName := Config.DB.DbName
	tablePrefix := Config.DB.TablePrefix

	db,err = gorm.Open(Config.DB.Type,fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
		user,
		password,
		host,
		dbName))
	if err != nil{
		panic(err)
	}
	gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
		return tablePrefix + defaultTableName
	}
	//db.Callback().Create().Replace("gorm:update_time_stamp", fieldTypeForSelect)
	db.LogMode(Config.DB.Mode)
	db.SingularTable(true)//禁用表名复数
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)
}
